package com.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.common.Criteria;
import com.common.GridReturn;
import com.util.JSONUtil;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.base.BaseController;
import com.model.User;
import com.service.UserService;
import com.util.WebConstants;

/**
 * 控控制层
 * @author tiangai
 */
@Controller
@RequestMapping("/user") 
public class UserController extends BaseController {
	@Resource
	private UserService userService;

	

    @RequestMapping(value="/index")
    public ModelAndView index() {
        Map<String,Object>  context = new HashMap<String, Object>();
        return forword("user/user",context);
    }
    @RequestMapping(value="/list")
    public void list(Criteria criteria, HttpServletResponse response) {

        int total=userService.getTotal(criteria);
        System.out.println("==============");
        System.out.println(criteria.getAsString("sex"));
        System.out.println(criteria.getAsString("account"));
        criteria.setRowCount(total);
        List list= userService.selectByCriteriaPagination(criteria);
        writerJson(response, new GridReturn(total,list).toString());
    }
	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public ModelAndView logout(HttpSession session) {
		Map<String,Object>  context = getRootMap();
		session.setAttribute(WebConstants.CURRENT_USER, null);
		return  forword("login",context);
	}
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    public void delete(String ids,HttpServletResponse response) {
        //将json字符串转成int []
       int [] id= JSONUtil.fromJson(ids,new TypeReference<int []>() {});
        userService.deleteBatch(id);
        writerJson(response, successMsg("删除成功"));

    }

}
